<?php
/*
 *@des 登录控制器
 *@author www.speakphp.com
 *@time 2015-09-07  
 */
class LoginAction extends AdmincommonAction {
	public function __construct(){
		parent::__construct();	
	}
    public function index(){
    	$this->_is_login();
    	if($this->is_login){
    		header("Location:".U("index/index"));
    	}
      $bgData = array(
        'login_bg_01.jpg',
        'login_bg_02.jpg',
        'login_bg_03.jpg',
        'login_bg_04.png',
        'login_bg_05.jpg',
        'login_bg_06.jpg',
        'login_bg_07.jpg',
        'login_bg_08.jpg',
        'login_bg_09.jpg',
        'login_bg_10.jpg',
        'login_bg_11.jpg',
        'login_bg_12.jpg',
      );
      
      $bgpic = APP_TMPL_PATH."/static/images/".$bgData[array_rand($bgData,1)];
      $this->assign("bgpic" , $bgpic);
    	$this->display();
    }
    public function verify(){
    	import('ORG.Util.Image');
    	Image::buildImageVerify(4 , 1 , 'png' , 100 , 40);
    }
    public function do_login(){
    	$d = D("Admin/AdminUser");
    	$username = trim($this->_request("username"));//name
    	$password =trim($this->_request("pwd"));//passwd
  		if(empty($username)){
  			$this->error("用户名不可以为空");
  		}elseif(empty($password)){
  			$this->error("密码不可以为空") ;
  		}
  
  		if($_SESSION['verify'] != md5(trim($_POST['code']))) {
  			$this->error('验证码错误！');
  		}
    	$res  = $d->fineUser($username , $password);
    	if(!$res['status']){
    		$this->error($res['message']);
    	}
    	
    	$res = $d->setUserCookie($res['info']['id']);
		if(!$res['status']){
			$this->error($res['message']);
		}
    	//登录成功
    	header("Location:".U("index/index"));
    }
    //退出登录
    public function login_out(){
    	cookie(null); // 清空当前设定前缀的所有cookie值
    	cookie(null,C('cookie_prefix')); //  清空指定前缀的所有cookie值
    	cookie('admin_auth',null);
    	header("Location:".U("login/index"));
    }
    //校验用户名的合法性
    public function checkUsernamePwd(){
      $username = trim($this->_request("username"));//
      $pwd = trim($this->_request("pwd"));//
      $d = D("Admin/AdminUser");
      $res  = $d->fineUser($username , $pwd);
      if(!$res['status']){
        echo json_encode(array('status'=>0 , 'message'=>$res['message']));
      }else{
        echo json_encode(array('status'=>1 , 'message'=>"success"));
      }
    }
    function checkCode(){
      $code = trim($this->_request("code"));
      if($_SESSION['verify'] != md5(trim($code))) {
        echo json_encode(array('status'=>0 , 'message'=>"验证码错误！"));
      }else{
        echo json_encode(array('status'=>1 , 'message'=>"success"));
      }
    }
    
}